﻿using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Quant.Services;
using Microsoft.Extensions.Configuration;

try
{
    Console.WriteLine("Host starting...");
    try
    {
        var host = Host.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.SetBasePath(Directory.GetCurrentDirectory());
                config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
            })
            .ConfigureLogging(logging =>
            {
                logging.ClearProviders();
                logging.AddConsole();
            })
            .ConfigureServices(services =>
            {

                services.AddSingleton<BinanceService>();
                services.AddHttpClient();
                // services.AddSingleton<IConfiguration>(sp =>
                // {
                //     Console.WriteLine($"{i += 1}");
                //     var t = sp.GetRequiredService<IConfiguration>();
                //     return t;
                // });
                services.AddMemoryCache();
                //services.AddDbContext<AppDbContext>();
                services.AddSingleton<CoreEngine>();
                services.AddHostedService<TradeBackgroundService>();
            })
            .Build();
        Console.WriteLine("Host created successfully.");
        host.Run();
        Console.WriteLine("Host stopped successfully.");
    }
    catch (Exception innerEx)
    {
        Console.WriteLine($"Host failed during setup or running: {innerEx}");
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Outer exception caught: {ex}");
}


//try
//{
//    Console.WriteLine("Host starting...");
//    try
//    {
//        var host = Host.CreateDefaultBuilder(args)
//            .ConfigureServices((context, services) =>
//            {
//                //services.AddSingleton<BinanceService>();
//                services.AddHttpClient();
//                //services.AddSingleton<IConfiguration>(sp => sp.GetRequiredService<IConfiguration>());
//                //services.AddMemoryCache();
//                // 恢复AppDbContext的注册
//                //services.AddDbContext<AppDbContext>();
//                //services.AddSingleton<CoreEngine>();
//                //services.AddHostedService<TradeBackgroundService>();
//            })
//            .Build();
//        Console.WriteLine("Host created successfully.");
//        Console.WriteLine("Host starting...1");
//        host.Run();
//        Console.WriteLine("Host stopped successfully.");
//        Console.WriteLine("Host starting...2");
//    }
//    catch (Exception innerEx)
//    {
//        Console.WriteLine($"Host failed during setup or running: {innerEx}");
//    }
//}
//catch (Exception ex)
//{
//    Console.WriteLine($"Outer exception caught: {ex}");
//}
